package com.easylinkin.linkappapi.knowledgebase.controller;


import com.baomidou.mybatisplus.core.metadata.IPage;
import com.easylinkin.linkappapi.annotation.CommonOperateLogAnnotate;
import com.easylinkin.linkappapi.common.model.RequestModel;
import com.easylinkin.linkappapi.knowledgebase.entity.KnowledgeBase;
import com.easylinkin.linkappapi.knowledgebase.service.KnowledgeBaseService;
import com.easylinkin.linkappapi.knowledgebase.vo.BatchAddVO;
import com.easylinkin.linkappapi.knowledgebase.vo.BatchUpateTypeVO;
import com.easylinkin.linkappapi.operatelog.constant.LogConstant.LogModule;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.Assert;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import site.morn.rest.RestBuilders;
import site.morn.rest.RestMessage;

/**
 * <p>
 * 知识库表 前端控制器
 * </p>
 *
 * @author xy
 * @since 2021-12-02
 */
@RestController
@Api(value = "知识库控制器", tags = {"知识库控制器"})
@RequestMapping("/knowledgebase/knowledgeBase")
public class KnowledgeBaseController {

  @Autowired
  private KnowledgeBaseService service;

  @PostMapping("getPage")
  @ApiOperation("查询知识库分页列表")
  public RestMessage getKnowledgeBasePage(@RequestBody RequestModel<KnowledgeBase> requestModel) {
    Assert.notNull(requestModel.getCustomQueryParams(), "customQueryParams 不能为空");
    Assert.notNull(requestModel.getPage(), "page 不能为空");
    IPage<KnowledgeBase> record = service
        .getKnowledgeBasePage(requestModel.getPage(), requestModel.getCustomQueryParams());
    return RestBuilders.successBuilder().data(record).build();
  }

  /**
   * 新增知识
   */
  @ApiOperation("新增知识")
  @PostMapping("add")
  @CommonOperateLogAnnotate(module = LogModule.KNOWLEDGE_BASE, desc = "新增知识")
  public RestMessage addKnowledgeBase(@RequestBody KnowledgeBase knowledgeBase) {
    checkParams(knowledgeBase);
    service.addKnowledgeBase(knowledgeBase);
    return RestBuilders.successBuilder().build();
  }

  /**
   * 更新知识类别
   */
  @ApiOperation("更新知识")
  @PutMapping("update")
  @CommonOperateLogAnnotate(module = LogModule.KNOWLEDGE_BASE, desc = "修改知识")
  public RestMessage updateKnowledgeBase(@RequestBody KnowledgeBase knowledgeBase) {
    checkParams(knowledgeBase);
    Assert.notNull(knowledgeBase.getId(), "id不能为空");
    service.updateKnowledgeType(knowledgeBase);
    return RestBuilders.successBuilder().build();
  }

  /**
   * 根据id删除知识
   */
  @ApiOperation("根据id删除删除知识")
  @PostMapping("deleteKnowledgeBaseById")
  @CommonOperateLogAnnotate(module = LogModule.KNOWLEDGE_BASE, desc = "删除知识")
  public RestMessage deleteKnowledgeTypeById(@RequestBody KnowledgeBase knowledgeBase) {
    Assert.notNull(knowledgeBase.getId(), "ID不能为空");
    service.deleteKnowledgeBaseById(knowledgeBase.getId());
    return RestBuilders.successBuilder().build();
  }

  /**
   * 校验数据
   */
  private void checkParams(KnowledgeBase knowledgeBase) {
    Assert.notNull(knowledgeBase, "请求参数不能为空");
    Assert.notNull(knowledgeBase.getName(), "标题不能为空");
    Assert.notNull(knowledgeBase.getContent(), "内容不能为空");
    Assert.notNull(knowledgeBase.getPermissionScope(), "访问权限不能为空");
    if (null != knowledgeBase.getPermissionScope() && 1 == knowledgeBase.getPermissionScope()) {
      Assert.notNull(knowledgeBase.getPermissionUsers(), "可访问人员不能为空");
    }
  }

  /**
   * 批量新增知识
   */
  @ApiOperation("批量新增知识")
  @PostMapping("batchAdd")
  @CommonOperateLogAnnotate(module = LogModule.KNOWLEDGE_BASE, desc = "批量新增知识")
  public RestMessage batchAdd(@RequestBody BatchAddVO batchAddVO) {
    Assert.notNull(batchAddVO, "请求参数不能为空");
    Assert.notNull(batchAddVO.getAttachments(), "附件列表不能为空");
    Assert.notNull(batchAddVO.getTypes(), "类型列表不能为空");
    Assert.notNull(batchAddVO.getPermissionScope(), "访问权限不能为空");
    if (null != batchAddVO.getPermissionScope() && 1 == batchAddVO.getPermissionScope()) {
      Assert.notNull(batchAddVO.getPermissionUsers(), "可访问人员不能为空");
    }
    service.batchAdd(batchAddVO);
    return RestBuilders.successBuilder().build();
  }

  /**
   * 批量修改知识所属分类
   */
  @ApiOperation("批量修改知识所属分类")
  @PostMapping("batchUpdateType")
  @CommonOperateLogAnnotate(module = LogModule.KNOWLEDGE_BASE, desc = "批量修改知识所属分类")
  public RestMessage batchUpdateType(@RequestBody BatchUpateTypeVO batchUpateTypeVO) {
    Assert.notNull(batchUpateTypeVO, "请求参数不能为空");
    Assert.notNull(batchUpateTypeVO.getIds(), "知识id列表不能为空");
    Assert.notNull(batchUpateTypeVO.getTypes(), "知识分类不能为空");
    Assert.notNull(batchUpateTypeVO.getMoveWay(), "移动方式不能为空");
    service.batchUpdateType(batchUpateTypeVO);
    return RestBuilders.successBuilder().build();
  }

  /**
   * 下载知识所有附件
   */
  @ApiOperation("下载知识所有附件")
  @PostMapping("downloadAttachment")
  @CommonOperateLogAnnotate(module = LogModule.KNOWLEDGE_BASE, desc = "下载知识所有附件")
  public RestMessage downloadAttachment(@RequestBody KnowledgeBase knowledgeBase) {
    Assert.notNull(knowledgeBase.getId(), "ID不能为空");
    String fileName = service.downloadAttachment(knowledgeBase.getId());
    return RestBuilders.successBuilder().data(fileName).build();
  }

  /**
   * 知识详情
   */
  @ApiOperation("知识详情")
  @GetMapping("getDetailById/{id}")
  @CommonOperateLogAnnotate(module = LogModule.KNOWLEDGE_BASE, desc = "知识详情")
  public RestMessage getDetailById(@PathVariable("id") Long id) {
    KnowledgeBase knowledgeBase = service.getDetailById(id);
    return RestBuilders.successBuilder().data(knowledgeBase).build();
  }


}
